package com.elong.businesstravel.base.unionlogin.qq;

import java.util.Map;
import java.util.Set;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.elong.businesstravel.base.unionlogin.UnionLoginSucceed;
import com.elong.businesstravel.entitys.UnionLoginInfo;
import com.elong.businesstravel.include.Version;
import com.elong.businesstravel.protocol.ProtocolDef;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.RequestType;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.UMSsoHandler;
import com.umeng.socialize.controller.listener.SocializeListeners.UMAuthListener;
import com.umeng.socialize.controller.listener.SocializeListeners.UMDataListener;
import com.umeng.socialize.exception.SocializeException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

/**
 * qq联合登陆
 * 
 * @author lenovo
 * 
 */
public class QQUnionLogin {

	/** 1- 微博, 2 - QQ, 3 - 微信 */
	public static final String OPEN_TYPE = "2";

	private static String APP_ID = "100539397";

	private Context mContext;
	private String mAppid;

	private UMSocialService mUmSocialService;
	private UnionLoginSucceed mUnionLoginSucceed;

	public QQUnionLogin(Context context) {
		mContext = context;

		initQQUnionLogin();
	}

	private void initQQUnionLogin() {

		mAppid = APP_ID;
		mUmSocialService = UMServiceFactory.getUMSocialService(
				Version.productName, RequestType.SOCIAL);

		mUmSocialService.getConfig().supportQQPlatform((Activity) mContext,
				mAppid, ProtocolDef.SHARE_URL);

	}

	public void onClickLogin() {
		mUmSocialService.doOauthVerify(mContext, SHARE_MEDIA.QQ,
				new UMAuthListener() {

					@Override
					public void onError(SocializeException e,
							SHARE_MEDIA platform) {
					}

					@Override
					public void onComplete(Bundle value, SHARE_MEDIA platform) {
						if (value != null
								&& !TextUtils.isEmpty(value.getString("uid"))) {
							HandlerToastUI.getHandlerToastUI(mContext, "授权成功");

							getPlatformInfo();
						} else {
							HandlerToastUI.getHandlerToastUI(mContext, "授权失败");
						}

					}

					@Override
					public void onCancel(SHARE_MEDIA platform) {
					}

					@Override
					public void onStart(SHARE_MEDIA platform) {
					}
				});

	}

	private void getPlatformInfo() {
		mUmSocialService.getPlatformInfo(mContext, SHARE_MEDIA.QQ,
				new UMDataListener() {

					@Override
					public void onStart() {
						// HandlerToastUI.getHandlerToastUI(mContext,
						// "获取平台数据开始...");
					}

					@Override
					public void onComplete(int status, Map<String, Object> info) {
						if (status == 200 && info != null) {
							
							StringBuilder sb = new StringBuilder();
							Set<String> keys = info.keySet();
							for (String key : keys) {
								sb.append(key + "=" + info.get(key).toString()
										+ "\r\n");
							}
							DebugLog.logI(sb.toString());
							
							UnionLoginInfo unionLoginInfo = new UnionLoginInfo();
							unionLoginInfo.screen_name = (String) info.get("screen_name");
							unionLoginInfo.openid = (String) info.get("openid");
							
//							unionLoginInfo.uid = (String) info.get("uid");
							unionLoginInfo.gender = (String) info.get("gender");
							unionLoginInfo.profile_image_url = (String) info.get("profile_image_url");
							unionLoginInfo.access_token = (String) info.get("access_token");
							unionLoginInfo.verified = (String) info.get("verified");
							if(null != mUnionLoginSucceed){
								mUnionLoginSucceed.onUnionLoginSucceed(unionLoginInfo);
							}
						} else {
							DebugLog.logI("发生错误：" + status);
						}
					}
				});

	}
	
	/**
	 * 联合登陆成功监听
	 * @param unionLoginSucceed
	 */
	public void setUnionLoginSucceedListener(UnionLoginSucceed unionLoginSucceed){
		mUnionLoginSucceed = unionLoginSucceed;
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		/** 使用SSO授权必须添加如下代码 */
		UMSsoHandler ssoHandler = mUmSocialService.getConfig().getSsoHandler(
				requestCode);
		if (ssoHandler != null) {
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
		}

	}

}
